<%@ taglib uri="/WEB-INF/tlds/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/tlds/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/tlds/struts-menu.tld" prefix="menu" %>
<%@ taglib uri="/WEB-INF/tlds/struts-menu-el.tld" prefix="menu-el" %>
<%@ taglib uri="/WEB-INF/tlds/c.tld" prefix="c" %>
<%@ taglib uri="/WEB-INF/tlds/fmt.tld" prefix="fmt" %>
<%@ taglib uri="/WEB-INF/tlds/authz.tld" prefix="authz" %>
<%@ taglib uri="/WEB-INF/tlds/sitemesh-decorator.tld" prefix="decorator"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" scope="request"/>
<script type="text/javascript" src="${ctx}/dwr/engine.js"></script>
<script type="text/javascript" src="${ctx}/dwr/util.js"></script>
<script type="text/javascript">
<!--
	function addAppli() {
		var name = $F("name");
		var list = $("appliList");		
		name = trim(name);
		spec = unAllowedChars(name,'<bean:message key="allowedCharacters"/>');
		$("name").value=name;
		if(!name || name.length == 0) {
			alert('<bean:message key="error.nameFieldEmpty" />'); 
		} else if(spec) {
			alert('<bean:message key="error.nameContainsSpecialCharacter" />'); 
		} else if(contains(list,name)) {
			alert('<bean:message key="error.nameExistsInList"/>'); 
		} else {
			$('method').value='save';
			$("id").value='';
			$('applicationForm').submit();
		}
	}	
	function selectAppli() {
		$("name").value=$("appliList")[$("appliList").selectedIndex].text;
		$('id').value=$F("appliList");
		$('name').focus();
	}
	
	function modifyAppli() {
		var name = $F("name");
		var list = $("appliList");
		var listVal = $F("appliList");
		name = trim(name);
		spec = unAllowedChars(name,'<bean:message key="allowedCharacters"/>');
		$("name").value=name;
		if(!listVal || listVal.length==0) {
			alert('<bean:message key="info.selectApplicationToModify"/>');
		} else if(!name || name.length==0) {
			alert('<bean:message key="error.nameFieldEmpty" />');
		} else if(spec) {		
			alert('<bean:message key="error.nameContainsSpecialCharacter" />'); 			
		} else if(name == $("appliList")[$("appliList").selectedIndex].text) {		
			alert('<bean:message key="error.nameSameSelectedApplication"/>');			
		} else if(contains(list,name)) {
			alert('<bean:message key="error.nameExistsInList"/>'); 
		} else {
			$('method').value='save';
			$('applicationForm').submit();
		}	
	}	
	function deleteAppli() {
		var listVal = $F("appliList");
		if(!listVal || listVal.length==0) {
			alert('<bean:message key="info.selectApplicationToDelete"/>');
		} else if(confirm('<bean:message key="info.confirmDeletion"/>')) {
			$('method').value='delete';
			$('applicationForm').submit();
		}
	}	
// -->					
</script>
<h1><bean:message key="applicationsPage.title"/></h1>
<div id="subcontent">
<html:form styleId="applicationForm" action="/application" focus="name">
		<input type="hidden" name="method" value="init" id="method" />
		<input type="hidden" name="forward" value="applications"/>
		<html:hidden styleId="id" property="application.id" />
		<p><bean:message key="applicationsPage.name" />: <html:text property="application.name" styleId="name" maxlength="127"/></p>
		<p>
			<select size="6" style="width:60%" name="appliList" id="appliList" onchange="selectAppli();" >
				<c:forEach var="application" items="${applications}">
					<option value="${application.id}"><c:out value="${application.name}" /></option>
				</c:forEach>
			</select>
		</p>
<authz:authorize ifAllGranted="ROLE_SUPERVISOR">
		<p>
			<input name="addAppliButton" type="submit" value="<bean:message key='applicationsPage.add' />" onclick="addAppli();return false;" />&nbsp;
			<input name="modifyAppliyButton" type="submit" value="<bean:message key='applicationsPage.modify' />" onclick="modifyAppli();return false;" />&nbsp;
			<input name="delAppliButton" type="submit" value="<bean:message key='applicationsPage.delete' />" onclick="deleteAppli();return false;" />
		</p>
</authz:authorize>		
</html:form>
</div>
<p>
</p>
